//
//  HomeDetailBuyViewController.m
//  GiftNote
//
//  Created by lanou on 15/8/14.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#define kBuyDetail @"http://api.liwushuo.com/v2/items/"  //详情

#import "HomeDetailBuyViewController.h"
#import "HomeHeader.h"
#import "HomeBuyHeadView.h"
#import "LORequestManger.h"
#import "HomeBuyDetailModel.h"

@interface HomeDetailBuyViewController ()<UIWebViewDelegate>


@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong)UIScrollView *mainScrollView;
@property (nonatomic, strong)HomeBuyDetailModel *buyModel;
@property (nonatomic, strong)UIWebView *desWebView;

@property (nonatomic, strong)HomeBuyHeadView *headView;
@property (nonatomic, strong)UIView *buyView;

@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, strong)NSMutableArray *commentArray;

@end

@implementation HomeDetailBuyViewController

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)commentArray
{
    if (_commentArray == nil) {
        self.commentArray = [NSMutableArray array];
    }
    return _commentArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self readData];
    
    self.navigationItem.title = @"礼物详情";
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 创建tableView

- (void)creatMainScrollView
{
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight - 40)];
    [self.view addSubview:_mainScrollView];
    
    _buyView = [[UIView alloc]initWithFrame:CGRectMake(0, kHomeHeight - 40, kHomeWidth, 40)];
    [self.view addSubview:_buyView];
    
    UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    buyButton.frame = CGRectMake(0, 0, kHomeWidth, 40);
    buyButton.backgroundColor = [UIColor redColor];
    [buyButton setTitle:@"去天猫购买" forState:UIControlStateNormal];
    [buyButton addTarget:self action:@selector(buyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_buyView addSubview:buyButton];
    
    
    _headView = [[HomeBuyHeadView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, [HomeBuyHeadView heightForView:_buyModel])];
    _headView.buyModel = _buyModel;
    [_mainScrollView addSubview:_headView];
    
    _desWebView = [[UIWebView alloc]init];
    _desWebView.delegate = self;
    [_desWebView loadHTMLString:_buyModel.detail_html baseURL:nil];
    [_mainScrollView addSubview:_desWebView];
}


#pragma mark - 数据解析

- (void)readData
{
    NSString *url = [kBuyDetail stringByAppendingString:[NSString stringWithFormat:@"%@",_ID]];
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *newDic = dic[@"data"];
        _buyModel = [[HomeBuyDetailModel alloc]init];
        [_buyModel setValuesForKeysWithDictionary:newDic];
        [self.dataSource addObject:_buyModel];
        [self creatMainScrollView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error:%@",error);
        
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    _desWebView.frame = CGRectMake(0, CGRectGetMaxY(_headView.frame) + 10, kHomeWidth, height);
    _mainScrollView.contentSize = CGSizeMake(kHomeWidth, height + 10 + [HomeBuyHeadView heightForView:_buyModel]);
}

- (void)buyButtonAction:(UIButton *)sender
{
    if (_buyModel.purchase_url != nil) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:_buyModel.purchase_url]];
    }
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
